//
//  WBStatusCell.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/18.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

class WBStatusCell: UITableViewCell {
    /// 头像
    @IBOutlet weak var iconView: UIImageView!
    /// 姓名
    @IBOutlet weak var nameLabel: UILabel!
    /// 会员
    @IBOutlet weak var memberIcon: UIImageView!
    /// 正文
    @IBOutlet weak var statusLabel: UILabel!
    /// VIP
    @IBOutlet weak var vipIcon: UIImageView!
    /// 来源
    @IBOutlet weak var sourceLabel: UILabel!
    /// 时间
    @IBOutlet weak var timeLabel: UILabel!
    /// 底部工具栏
    @IBOutlet weak var toolBar: WBStatusToolBar!
    /// 配图视图
    @IBOutlet weak var pictureView: WBStatusPictureView!
    /// 原创微博的cell并没有这个转发的label，所以要用可选类型
    @IBOutlet weak var retweetedLabel: UILabel?
    /// 微博视图模型
    var viewModel: WBStatusViewModel? {
        didSet {
            statusLabel.attributedText = viewModel?.statusAttrText?.az_setHomeText()
            retweetedLabel?.attributedText = viewModel?.retweetedAttrText
            nameLabel.text = viewModel?.status.user?.screen_name
            nameLabel.textColor = viewModel?.nameLabelColor
            memberIcon.image = viewModel?.memberIcon
            vipIcon.image = viewModel?.vipIcon
            iconView.az_setImage(urlString: viewModel?.status.user?.avatar_large, placeholderImage: UIImage(named: "avatar_default_big"), isAvatar: true)
            toolBar.viewModel = viewModel
            pictureView.viewModel = viewModel
            pictureView.urls = viewModel?.picURLs
            sourceLabel.text = viewModel?.status.source
            timeLabel.text = viewModel?.status.create_date?.az_dateDescription
            timeLabel.textColor = viewModel?.status.create_date?.dateColor
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
     
        // 异步绘制-离屏渲染
        self.layer.drawsAsynchronously = true
        // 栅格化
        self.layer.shouldRasterize = true
        // 需要指定分辨率
        self.layer.rasterizationScale = UIScreen.main.scale
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

extension NSAttributedString {
    func az_setHomeText() -> NSMutableAttributedString {
    
        let attr = NSMutableAttributedString(attributedString: self)
        
        let para = NSMutableParagraphStyle()
        para.lineSpacing = 4
        
        attr.addAttribute(NSParagraphStyleAttributeName, value: para, range: NSRange(location: 0, length: attr.length))
        
        return attr
    }
}
